<template>
  <div :class="['login-container', { 'style-left': isLeftStyle }]">
    <div v-if="!isLeftStyle" class="header">
      <div class="content">
        <dl>
          <dt>欢迎使用{{ VITE_APP_NAME }}业务管理系统</dt>
          <dd>更方便 / 更及时 / 更快速</dd>
        </dl>
        <div class="pic"></div>
      </div>
    </div>
    <div class="code">
      <dl>
        <dt class="flex center">
          <img v-if="isLeftStyle" class="logo" :src="LOGO" alt="" />
          <div v-else>{{ VITE_APP_NAME }}业务管理系统</div>
        </dt>
      </dl>
      <div class="iframe">
        <div v-if="!isLeftStyle" class="title">
          <i class="x-icon icon-016" />
          <strong>钉钉扫码登更安全</strong>
        </div>
        <iframe :src="iframeSrc" frameborder="0"></iframe>
      </div>
    </div>

    <div v-if="isLeftStyle" class="pic-box">
      <dl class="flex column center">
        <dt>欢迎使用和气业务管理系统web3.0</dt>
        <dd>更方便 / 更及时 / 更快速</dd>
      </dl>
      <img class="pic" :src="IMG_LOGIN" alt="" />
    </div>
  </div>
</template>

<script lang="ts">
  import { computed, defineComponent, reactive } from 'vue'
  import { useRoute } from 'vue-router'
  import LOGO from '@/assets/images/login/logo-hq.svg'
  import IMG_LOGIN from '@/assets/images/login/xth-hq.png'

  export default defineComponent({
    name: 'Login',
    components: {},
    setup() {
      const { VITE_HOSTNAME, VITE_DINGTALK_APP_ID, VITE_APP_NAME } = import.meta.env
      const isLeftStyle = VITE_APP_NAME === '和气生财'
      const config = {
        origin: `https://${VITE_HOSTNAME}`,
        appId: VITE_DINGTALK_APP_ID || 'dingoa62xp9lke1mjaiinr'
      }

      const $route = useRoute()
      const $state = reactive({
        redirectRri: computed(() => {
          const url = [`${config.origin}/oauth`]
          const query = []
          // 处理非正式环境下url间的跳转
          if (window.location.origin !== config.origin) {
            query.push(`origin=${encodeURIComponent(window.location.origin)}`)
          }
          if ($route.query.redirect) {
            query.push(`redirect=${encodeURIComponent($route.query.redirect)}`)
          }
          url.push(query.join('&'))
          return encodeURIComponent(url.join('?'))
        }),
        iframeSrc: computed(() => {
          console.log($state.redirectRri)
          const goto = encodeURIComponent(
            `https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${config.appId}&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=${$state.redirectRri}`
          )
          const style = encodeURIComponent('border:none;background-color:#FFFFFF;')
          return `https://login.dingtalk.com/login/qrcode.htm?goto=${goto}&style=${style}`
        })
      })

      const initScanCodeLogin = () => {
        const handleMessage = (event: { origin: string; data: string }) => {
          const origin = event.origin
          // console.log('initScanCodeLogin', event)
          if (origin === 'https://login.dingtalk.com') {
            // 判断是否来自ddLogin扫码事件。
            const loginTmpCode = event.data
            // 拿到loginTmpCode后就可以在这里构造跳转链接进行跳转了
            window.location.href = `https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=${config.appId}&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=${$state.redirectRri}&loginTmpCode=${loginTmpCode}`
          }
        }
        if (typeof window.addEventListener !== 'undefined') {
          window.addEventListener('message', handleMessage, false)
        } else if (typeof window.attachEvent !== 'undefined') {
          window.attachEvent('onmessage', handleMessage)
        }
      }
      initScanCodeLogin()
      return {
        iframeSrc: $state.iframeSrc,
        VITE_APP_NAME,
        isLeftStyle,
        LOGO,
        IMG_LOGIN
      }
    }
  })
</script>

<style lang="scss" scoped>
  .login-container {
    height: 100vh;
    .header {
      height: 35vh;
      background-color: #f0f3ff;
      .content {
        max-width: 1200px;
        padding: 0 20px;
        margin: 0 auto;
        height: 100%;
        display: flex;
        align-items: center;
        dl {
          flex: 1;
          dt {
            color: #333333;
            font-weight: bold;
            padding-bottom: 10px;
            font-size: 32px;
          }
          dd {
            color: #80878b;
            font-size: 28px;
          }
        }
        .pic {
          width: 500px;
          height: 100%;
          background: url('../../assets/images/login/bms.png') no-repeat center 20px;
          background-size: cover;
        }
      }
    }
    .code {
      width: 550px;
      background: #ffffff;
      box-shadow: 0 4px 8px rgba(210, 218, 255, 0.6);
      border-radius: 10px;
      position: relative;
      margin: -50px auto 0;
      text-align: center;
      padding: 50px;

      .logo {
        width: 250px;
        margin-bottom: -65px;
        z-index: 1;
      }
      dl {
        padding-bottom: 20px;
        dt {
          color: #536fff;
          font-size: 28px;
          font-weight: bold;
        }
        dd {
          color: #7a7a8d;
          font-size: 18px;
        }
      }
      .iframe {
        display: inline-block;
        overflow: hidden;
        position: relative;
        padding-top: 20px;

        .title {
          position: absolute;
          left: 50%;
          top: 0;
          transform: translate(-50%, 0);
          line-height: 1;
          font-size: 24px;
          font-weight: 300;
          display: flex;
          align-items: center;
          justify-content: center;
          z-index: 3;
          strong {
            padding-left: 10px;
            font-weight: normal;
            white-space: nowrap;
          }
          .x-icon {
            background-color: #3296fa;
            width: 32px;
            height: 32px;
            border-radius: 50%;
            font-size: 24px;
            color: #fff;
            display: flex;
            align-items: center;
            justify-content: center;
            text-align: center;
          }
        }
      }
      iframe {
        width: 300px;
        height: 300px;
      }
    }

    &.style-left {
      background: linear-gradient(90deg, #f0f3ff 30vw, #ffffff 30vw);

      .code {
        position: absolute;
        width: 450px;
        height: unset;
        padding: 60px 30px;
        box-shadow: 0 0 22px rgba(41, 107, 239, 0.49);
        border-radius: 22px;
        left: calc(30vw - 226px);
        top: 50%;
        transform: translate3d(0, -50%, 0);
      }
    }

    .pic-box {
      position: absolute;
      left: calc(30vw + 226px + 140px);
      top: 40%;
      transform: translate3d(0, -40%, 0);
      width: 40vw;
      min-width: 540px;
      max-width: 778px;

      dl {
        margin-bottom: 40px;
        dt {
          color: #333333;
          font-weight: bold;
          font-size: 40px;
        }
        dd {
          color: #80878b;
          font-size: 30px;
        }
      }
    }
  }

  @media (max-width: 1000px) {
    .login-container {
      .header {
        height: 25vh;
        .content {
          display: none;
        }
      }
    }
  }

  @media (max-width: 750px) {
    .login-container {
      .header {
        display: none;
      }

      .code {
        width: auto;
        border-radius: 0;
        box-shadow: none;
        padding-top: 20vh;
        .iframe {
          overflow: initial;
          iframe {
            position: absolute;
            width: 300px;
            height: 300px;
            left: 50%;
            transform: translate(-50%, 0);
          }
        }
      }

      &.style-left {
        .code {
          width: 550px;
          background: #ffffff;
          box-shadow: 0 4px 8px rgba(210, 218, 255, 0.6);
          border-radius: 10px;
          position: relative;
          margin: -50px auto 0;
          text-align: center;
          padding: 50px;

          .iframe {
            overflow: hidden;
            iframe {
              position: relative;
              width: 300px;
              height: 300px;
              left: unset;
              transform: unset;
            }
          }
        }
      }
    }
  }
</style>
